

package com.mt.mall.controller.merchant;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.merchant.FollowStoreEditDto;
import com.mt.mall.entity.merchant.FollowStore;
import com.mt.mall.service.merchant.FollowStoreService;
import com.mt.mall.service.merchant.ShopService;
import com.mt.common.system.service.AttachmentService;
import com.mt.common.system.service.UserService;


@Api(tags = "关注店铺")
@RestController
@RequestMapping("/api/mall/merchant/FollowStore")
@CrossOrigin(allowCredentials = "true")
public class FollowStoreController {
    private static Logger logger = LoggerFactory.getLogger(FollowStoreController.class);


    @Autowired
    private FollowStoreService followStoreService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private UserService userService;

    /**
     * 根据分页参数查询关注店铺集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:view')")
    @ApiOperation("分页查询关注店铺")
    @ApiPageParam
    @PostMapping("/findFollowStores")
    public PageResultDTO findFollowStores(@RequestBody PageDTO pageDTO) {
        return this.followStoreService.findFollowStores(pageDTO);
    }

    /**
     * 根据ID查询指定的关注店铺
     *
     * @param followStoreId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:view')")
    @ApiOperation("根据id查询关注店铺")
    @ApiPageParam
    @PostMapping("/findFollowStore")
    public FollowStore findFollowStore(@RequestParam Long followStoreId) {
        return this.followStoreService.findFollowStore(followStoreId);
    }

    /**
     * 根据ID查询指定的关注店铺(包含外键名称)
     *
     * @param followStoreId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:view')")
    @ApiOperation("根据ID查询指定的关注店铺(包含外键名称)")
    @PostMapping("/findFollowStoreForView")
    public FollowStore findFollowStoreForView(@RequestParam Long followStoreId) {
        return this.followStoreService.findFollowStoreWithForeignName(followStoreId);
    }

    /**
     * 根据ID查询指定的关注店铺(包含关注店铺和外键名称)
     *
     * @param followStoreId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:view')")
    @ApiOperation("根据ID查询指定的关注店铺(包含关注店铺和外键名称)")
    @PostMapping("/findFollowStoreForEdit")
    public FollowStoreEditDto findFollowStoreForEdit(@RequestParam Long followStoreId) {
        FollowStoreEditDto followStoreEditDto = new FollowStoreEditDto();
        followStoreEditDto.setFollowStore(this.followStoreService.findFollowStoreWithForeignName(followStoreId));

        this.prepareFollowStoreEditDto(followStoreEditDto);

        return followStoreEditDto;
    }

    /**
     * 根据ID查询指定的关注店铺(只提取ID 和 Name)
     *
     * @param followStoreId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:view')")
    @ApiOperation("根据ID查询指定的关注店铺(只提取ID 和 Name)")
    @PostMapping("/findFollowStoresWithIdNameById")
    public FollowStore findFollowStoresWithIdNameById(@RequestParam Long followStoreId) {
        return this.followStoreService.findFollowStoresWithIdNameById(followStoreId);
    }

    /**
     * 根据名称查询关注店铺集合(只提取ID 和 Name)
     *
     * @param followStoreName 名称
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:view')")
    @ApiOperation("根据名称查询关注店铺集合(只提取ID 和 Name)")
    @PostMapping("/findFollowStoresWithIdNameByName")
    public List<FollowStore> findFollowStoresWithIdNameByName(@RequestParam String followStoreName) {
        return this.followStoreService.findFollowStoresWithIdNameByName(followStoreName);
    }


    /**
     * 创建新的关注店铺
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:add')")
    @ApiOperation("创建新的关注店铺")
    @PostMapping("/createFollowStore")
    public FollowStoreEditDto createFollowStore() {
        FollowStoreEditDto followStoreEditDto = new FollowStoreEditDto();
        followStoreEditDto.setFollowStore(new FollowStore());

        this.prepareFollowStoreEditDto(followStoreEditDto);
        return followStoreEditDto;
    }

    private void prepareFollowStoreEditDto(FollowStoreEditDto followStoreEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        followStoreEditDto.setShopShops(this.shopService.findAllShopsWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        followStoreEditDto.setAttachmentsAttachments(this.attachmentService.findAllAttachmentsWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        followStoreEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
    }

    /**
     * 新增保存关注店铺
     *
     * @param followStore 实体对象
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:add')")
    @ApiOperation("新增保存关注店铺")
    @PostMapping("/saveFollowStore")
    public FollowStore saveFollowStore(@RequestBody FollowStore followStore) {
        return this.followStoreService.saveFollowStore(followStore);
    }

    /**
     * 修改保存关注店铺
     *
     * @param followStore 实体对象
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:update')")
    @ApiOperation("修改保存关注店铺")
    @PostMapping("/updateFollowStore")
    public FollowStore updateFollowStore(@RequestBody FollowStore followStore) {
        return this.followStoreService.updateFollowStore(followStore);
    }

    /**
     * 根据ID删除关注店铺
     *
     * @param followStoreId ID
     */
    @PreAuthorize("hasAuthority('mall:merchant:FollowStore:remove')")
    @ApiOperation("根据ID删除关注店铺")
    @PostMapping("/deleteFollowStore")
    public void deleteFollowStore(@RequestParam Long followStoreId) {
        this.followStoreService.deleteFollowStore(followStoreId);
    }


}

